package org.zfes.snowier.cloud.zbss.cms.checkFlow.service.impl;

import java.util.List;
import java.util.Map;
import java.util.Optional;

import org.zfes.snowier.core.consts.AppBizConst;
import org.zfes.snowier.core.data.DataSet;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.zfes.snowier.core.util.ZBeanUtil;
import org.zfes.snowier.core.util.ZDateUtil;
import org.zfes.snowier.core.util.ZAlert;
import org.zfes.snowier.core.util.ZAssert;
import org.zfes.snowier.common.dao.params.ParamMap;
import org.zfes.snowier.cloud.zbss.cms.checkFlow.service.ICmsCheckFlowStepsService;
import org.zfes.snowier.cloud.zbss.cms.checkFlow.service.ICmsCheckProcessService;
import org.zfes.snowier.cloud.zbss.cms.checkFlow.dao.CmsCheckFlowStepsMapper;
import org.zfes.snowier.cloud.zbss.cms.checkFlow.dao.CmsCheckProcessMapper;
import org.zfes.snowier.cloud.zbss.cms.checkFlow.model.CmsCheckFlowSteps;
import org.zfes.snowier.cloud.zbss.cms.checkFlow.model.CmsCheckProcess;

import java.util.stream.Stream;
@Service
public class CmsCheckFlowStepsServiceImpl implements ICmsCheckFlowStepsService{
 	@Autowired
	private CmsCheckFlowStepsMapper cmsCheckFlowStepsMapper;
 	
 	@Autowired
 	private CmsCheckProcessMapper cmsCheckProcessMapper;
	
	@Transactional(rollbackFor=Exception.class)
 	@Override
	public void createCmsCheckFlowSteps(CmsCheckFlowSteps cmsCheckFlowSteps) {
		ZBeanUtil.validateBean(cmsCheckFlowSteps);
		cmsCheckFlowSteps.setStatus(Byte.valueOf("1"));
		cmsCheckFlowSteps.setCreateTime(ZDateUtil.newDate());
		cmsCheckFlowSteps.setUpdateTime(ZDateUtil.newDate());
		cmsCheckFlowStepsMapper.insertSelective(cmsCheckFlowSteps);
	}
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public Optional<CmsCheckFlowSteps> loadCmsCheckFlowStepsById(Long id) {
		if(id!=null){
			return Optional.ofNullable(cmsCheckFlowStepsMapper.selectByPrimaryKey(id));
		}
		return Optional.empty();
		
	}
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void updateCmsCheckFlowSteps(CmsCheckFlowSteps cmsCheckFlowSteps) {
		ZBeanUtil.validateBean(cmsCheckFlowSteps);
		cmsCheckFlowSteps.setUpdateTime(ZDateUtil.newDate());
		cmsCheckFlowStepsMapper.updateByPrimaryKeySelective(cmsCheckFlowSteps);
	}
	
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void deleteCmsCheckFlowSteps(Long id) {
		ZAssert.notNull(id, "请选择要操作的数据");
		cmsCheckFlowStepsMapper.deleteByPrimaryKey(id);
	}
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void setCmsCheckFlowStepsStatus(Long id, Byte status) {
		ZAssert.notNull(id, "请选择要操作的数据");
		ZAssert.notNull(status, "状态参数错误");
		if(!Stream.of(new Byte[] {-1,0,1}).anyMatch(val->val.equals(status))) {
			ZAlert.serviceLogicalError("状态参数错误");
		}
		cmsCheckFlowStepsMapper.updateStatus( id,  status) ;
	}
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public DataSet loadCmsCheckFlowStepsDataSet(Map<String, Object> params) {
		ParamMap pm=ParamMap.filterParam(params);
		DataSet ds=DataSet.newDS2(cmsCheckFlowStepsMapper.selectListCount(pm), cmsCheckFlowStepsMapper.selectMapListPage(pm));
		return ds;
	}
	
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public List<CmsCheckFlowSteps> loadCmsCheckFlowStepsList(Long flowId,Long siteId){
		List<CmsCheckFlowSteps> cmsCheckFlowStepsList = cmsCheckFlowStepsMapper.selectCheckFlowStepsList(flowId,siteId,AppBizConst.STATUS.enabled);
		return cmsCheckFlowStepsList;
	}
	
	
	public Optional<CmsCheckFlowSteps> cmsCheckFlowFristStep(Long flowId,Long siteId,Long stepId){
		List<CmsCheckFlowSteps> cmsCheckFlowStepsList = cmsCheckFlowStepsMapper.selectCheckFlowStepsList(flowId,siteId,AppBizConst.STATUS.enabled);
		for (CmsCheckFlowSteps cmsCheckFlowSteps : cmsCheckFlowStepsList) {
			if(cmsCheckFlowSteps.getId() == stepId) {
				return Optional.ofNullable(cmsCheckFlowSteps);
			}
		}
		return Optional.empty();
	}
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void startProcessStep(Long flowId, Long siteId,Long contentId, Byte status) {
		CmsCheckFlowSteps cmsCheckFlowFristStep = cmsCheckFlowFristStep(flowId,siteId,Long.valueOf("2")).get();
		//想流程中插入一条数据
		CmsCheckProcess cmsCheckProcess = new CmsCheckProcess();
		for (String roleId : cmsCheckFlowFristStep.getCheckRoles().split(",")) {
			cmsCheckProcess.setCheckRole(Long.valueOf(roleId));
			cmsCheckProcess.setCheckStep(cmsCheckFlowFristStep.getId().intValue());
			cmsCheckProcess.setSiteId(siteId);
			cmsCheckProcess.setStatus(AppBizConst.STATUS.enabled);
			cmsCheckProcess.setCreateTime(ZDateUtil.newDate());
			cmsCheckProcess.setUpdateTime(ZDateUtil.newDate());
			cmsCheckProcess.setCheckFlowId(flowId);
			cmsCheckProcess.setContentId(contentId);
			cmsCheckProcessMapper.insertSelective(cmsCheckProcess);
		}
	}
	
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void nextProcessStep(Long stepIndex,Long contentId) {
		CmsCheckFlowSteps cmsCheckFlowNextStep = cmsCheckFlowStepsMapper.selectByPrimaryKey(stepIndex);
		//向流程中插入数据
		CmsCheckProcess cmsCheckProcess = new CmsCheckProcess();
		for (String roleId : cmsCheckFlowNextStep.getCheckRoles().split(",")) {
			cmsCheckProcess.setCheckRole(Long.valueOf(roleId));
			cmsCheckProcess.setCheckStep(cmsCheckFlowNextStep.getId().intValue());
			cmsCheckProcess.setSiteId(cmsCheckFlowNextStep.getSiteId());
			cmsCheckProcess.setStatus(AppBizConst.STATUS.enabled);
			cmsCheckProcess.setCreateTime(ZDateUtil.newDate());
			cmsCheckProcess.setUpdateTime(ZDateUtil.newDate());
			cmsCheckProcess.setCheckFlowId(cmsCheckFlowNextStep.getFlowId());
			cmsCheckProcess.setContentId(contentId);
			cmsCheckProcessMapper.insertSelective(cmsCheckProcess);
		}
	}
	
	
	
}